<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="文档ID" prop="documentId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.documentId"-->
      <!--          placeholder="请输入文档ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="分段序号" prop="segmentIndex">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.segmentIndex"-->
      <!--          placeholder="请输入分段序号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="字数统计" prop="wordCount">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.wordCount"-->
      <!--          placeholder="请输入字数统计"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="向量状态" prop="vectorStatus">
        <el-select v-model="queryParams.vectorStatus" placeholder="请选择向量状态" clearable>
          <el-option
            v-for="dict in dict.type.vector_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="向量模型" prop="vectorModel">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.vectorModel"-->
      <!--          placeholder="请输入向量模型"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['business:segment:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:segment:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:segment:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          icon="el-icon-edit"
          @click="addToSegment()"
          v-hasPermi="['business:document:edit']"
        >分段
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="segmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="文档ID" align="center" prop="documentId"/>-->
      <el-table-column label="文档标题" align="center" prop="documentTitle"/>
      <el-table-column label="分段序号" align="center" prop="segmentIndex"/>
      <el-table-column label="分段内容" align="center" prop="segmentContent" show-overflow-tooltip/>
      <!--      <el-table-column label="分段标题" align="center" prop="segmentTitle"/>-->
      <!--      <el-table-column label="分段摘要" align="center" prop="segmentSummary"/>-->
      <el-table-column label="字数统计" align="center" prop="wordCount"/>
      <!--      <el-table-column label="向量数据" show-overflow-tooltip align="center" prop="vectorData"/>-->
      <el-table-column label="向量状态" align="center" prop="vectorStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vector_status" :value="scope.row.vectorStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="createTime"/>
      <!--      <el-table-column label="${comment}" align="center" prop="remark"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:segment:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:segment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文档分段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--        <el-form-item label="分段ID" prop="id">-->
        <!--          <el-input v-model="form.id" placeholder="请输入分段ID"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="文档标题" prop="documentTitle">
          <el-input v-model="form.documentTitle" placeholder="请输入文档标题"/>
        </el-form-item>
        <el-form-item label="分段序号" prop="segmentIndex">
          <el-input v-model="form.segmentIndex" placeholder="请输入分段序号"/>
        </el-form-item>
        <el-form-item label="分段内容">
          <el-input type="textarea" v-model="form.segmentContent" :rows="5"/>
        </el-form-item>
        <!--        <el-form-item label="分段标题" prop="segmentTitle">-->
        <!--          <el-input v-model="form.segmentTitle" type="textarea" placeholder="请输入内容"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="分段摘要" prop="segmentSummary">-->
        <!--          <el-input v-model="form.segmentSummary" type="textarea" placeholder="请输入内容"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="字数统计" prop="wordCount">
          <el-input v-model="form.wordCount" placeholder="请输入字数统计"/>
        </el-form-item>
        <el-form-item label="向量状态" prop="vectorStatus">
          <el-select v-model="form.vectorStatus" placeholder="请选择向量状态">
            <el-option
              v-for="dict in dict.type.vector_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="向量模型" prop="vectorModel">-->
        <!--          <el-input v-model="form.vectorModel" placeholder="请输入向量模型"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSegment, getSegment, delSegment, addSegment, updateSegment} from "@/api/business/segment";
import {addToSegment} from "@/api/business/document";

export default {
  name: "Segment",
  dicts: ['vector_status'],
  props: {
    documentId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文档分段表格数据
      segmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        documentId: null,
        segmentIndex: null,
        segmentContent: null,
        segmentTitle: null,
        segmentSummary: null,
        wordCount: null,
        vectorData: null,
        vectorStatus: null,
        vectorModel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "分段ID不能为空", trigger: "blur"}
        ],
        documentId: [
          {required: true, message: "文档ID不能为空", trigger: "blur"}
        ],
        segmentIndex: [
          {required: true, message: "分段序号不能为空", trigger: "blur"}
        ],
        segmentContent: [
          {required: true, message: "分段内容不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 分段按钮操作 */
    addToSegment() {
      const that = this;
      this.$modal.confirm('是否确认将分段？').then(function () {
        return addToSegment({
          id: that.documentId,
        });
      }).then(() => {
        that.getList();
        that.$modal.msgSuccess("分段成功");
      }).catch(() => {
      });
    },
    /** 查询文档分段列表 */
    getList() {
      this.loading = true;
      if (this.documentId) {
        this.queryParams.documentId = this.documentId;
      }
      listSegment(this.queryParams).then(response => {
        this.segmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        documentId: null,
        segmentIndex: null,
        segmentContent: null,
        segmentTitle: null,
        segmentSummary: null,
        wordCount: null,
        vectorData: null,
        vectorStatus: null,
        vectorModel: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文档分段";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSegment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文档分段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSegment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSegment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文档分段编号为"' + ids + '"的数据项？').then(function () {
        return delSegment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/segment/export', {
        ...this.queryParams
      }, `segment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
